package com.kefu.exam.vo;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.kefu.common.context.KefuContext;
import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kefu.common.vo.AbstractSearchVo;
import com.kefu.exam.model.ExamSubject;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.List;

/**
 * @author xy
 * @date 2022/02/28
 */
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@ApiModel(description = "考题表")
public class ExamSubjectSearchVo extends AbstractSearchVo {

    @ApiModelProperty(value = "分类")
    private String typeId;

    @ApiModelProperty(value = "分类名")
    private String typeName;

    @ApiModelProperty(value = "考题状态")
    private Integer subjectStatus = 1;

    @ApiModelProperty(value = "试卷ID")
    private String paperId;

    @ApiModelProperty(value = "当前试卷下所有问题")
    private List<String> examSubject;

    @ApiModelProperty(value = "试题题干")
    private String subjectContent;


    @Override
    public LambdaQueryWrapper<ExamSubject> lambdaQuery() {
        LambdaQueryWrapper<ExamSubject> wrapper = Wrappers.<ExamSubject>lambdaQuery();
        if (StrUtil.isNotBlank(typeId)) {
            String[] idList = typeId.split(",");
            List<String> typeIdList = Lists.newArrayList(idList);
            wrapper.in(ExamSubject::getTypeId, typeIdList);//分类
        }
        if (StringUtils.isNotBlank(subjectContent)) {
            wrapper.like(ExamSubject::getSubjectContent, subjectContent);
        }
        wrapper.eq(ExamSubject::getTenantId, KefuContext.getKefuContext().getTenantId());
        wrapper.like(StringUtils.isNotBlank(typeName), ExamSubject::getTypeName, typeName);//分类名
        wrapper.eq(subjectStatus != null, ExamSubject::getSubjectStatus, subjectStatus);//考题状态
        if (examSubject != null && examSubject.size() > 0) {
            wrapper.notIn(!examSubject.isEmpty(), ExamSubject::getSubjectId, examSubject);
        }
        if (StringUtils.isNotBlank(paperId)){
            wrapper.and(w -> {
                w.isNull(ExamSubject::getFatherSubject)
                        .or()
                        .eq(ExamSubject::getFatherSubject, "");
            });
        }
        return wrapper;
    }
}